﻿namespace MicroCloud.Identity.Events
{
    /// <summary>
    /// 用户登录事件：登录日志
    /// </summary>
    public class LoginLogEventHandler : EventHandlerBase<LoginEventData>
    {
        //字段
        private readonly IRepository<LoginLog, long> _loginLogRepository;

        #region "构造函数"
        #region "初始化一个用户登录事件：登录日志的新实例"
        /// <summary>
        /// 初始化一个用户登录事件：登录日志 <see cref="LoginLogEventHandler"/> 的新实例
        /// </summary>
        /// <param name="loginLogRepository"></param>
        public LoginLogEventHandler(IRepository<LoginLog, long> loginLogRepository)
        {
            _loginLogRepository = loginLogRepository;
        }
        #endregion

        #endregion

        #region "方法"
        #region "事件处理"
        /// <summary>
        /// 事件处理
        /// </summary>
        /// <param name="eventData">事件源数据</param>
        /// <param name="provider">服务提供者</param>
        public override void Handle(LoginEventData eventData, IServiceProvider provider)
        {
            LoginLog log = new()
            {
                Ip = eventData.LoginDto.Ip,
                UserAgent = eventData.LoginDto.UserAgent,
                UserId = eventData.User.Id
            };
            _loginLogRepository.Insert(log);
        }
        #endregion

        #endregion

    }

}
